<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
require(ROOT_PATH . 'includes/lib_payment.php');
require(ROOT_PATH . 'includes/lib_order.php');
require_once(ROOT_PATH .'includes/modules/payment/wx_new_qrcode.php');
if($_POST['orderId']){
    $orderId = $_POST['orderId'];
    $data['status'] = $GLOBALS['db']->getOne('SELECT pay_status FROM ' . $GLOBALS['ecs']->table('order_info') ." WHERE order_sn = '$orderId'");
    if ($data['status'] == 0) {
        $data['info'] = '未付款';
    }elseif ($data['status'] == 1) {
        $data['info'] = '付款中';
    }elseif ($data['status'] == 2) {
        $data['info'] = '已付款';
    }
    echo json_encode($data);
    exit;
}



if($_REQUEST['step'] == 'respond'){
    $orderId = $_REQUEST['orderId']; //获取订单号
    $payment = get_payment('wx_new_qrcode');
    if ($orderId) {
        if (md5($payment['key'] + $orderId) != $_REQUEST['sign']) {
            show_message('签名错误！');
        }

        $order = $GLOBALS['db']->getRow('SELECT order_id,order_sn,pay_status  FROM ' . $GLOBALS['ecs']->table('order_info') ." WHERE order_sn = '$orderId'");
        $log_id =  $GLOBALS['db']->getOne('SELECT log_id FROM ' . $GLOBALS['ecs']->table('pay_log') ." WHERE order_id = '".$order["order_id"]."'");
        order_paid($log_id, 2);
        if ($order['pay_status'] == 2) {
            $msg = '支付成功';
        }else {
            $msg = '支付操作失败';
        }

    }else{
        show_message('没有找到该订单号！');
    }
    $smarty->assign('message',    $msg);
    $smarty->assign('shop_url',   $ecs->url());
    $smarty->display('respond.dwt');

} else {
    $orderId = $_REQUEST['orderId']; //获取订单号
    $product_id = intval( $_REQUEST['product_id']); //获取产品id
    is_numeric($orderId) or die('error order');
    $sql = 'SELECT * FROM ' . $GLOBALS['ecs']->table('order_info') ." WHERE order_sn = '$orderId'";
    $order = $GLOBALS['db']->getRow($sql);
    if (!$order) {
        show_message('该订单号没有找到！');
    }
    if ($order['pay_status'] == 2) {
        show_message('该订单已经支付成功！');
    }
    $order_id = $order['order_id'];
    $order['log_id'] = $GLOBALS['db']->getOne('SELECT log_id FROM ' . $GLOBALS['ecs']->table('pay_log') ." WHERE order_id = '$order_id'");
    $payment = payment_info($order['pay_id']);
    $pay_obj = new wx_new_qrcode();
    $order['product_id'] = $product_id;
// $pay_online = $pay_obj->get_code($order, unserialize_config($payment['pay_config']));
    $pay_online = $pay_obj->get_code($order, unserialize_config($payment['pay_config']));
    $order['pay_desc'] = $payment['pay_desc'];
    $smarty->assign('orderId', $orderId);
     $smarty->assign('order', $order);
    $smarty->assign('pay_online', $pay_online);
    $smarty->assign('sign', md5(WXKEY + $orderId));
    $smarty->display('wx_pay.dwt');
}




